<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="80px">
          <el-col :span="6">
      <el-form-item label="分类名称">
        <el-input
          v-model="listQuery.fclassstname"
          clearable
          class="filter-item"
          placeholder="请输入分类名称"
        />
      </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="上级分类">
        <el-select clearable filterable v-model="listQuery.fparentno" class="filter-item" placeholder="请选择上级分类">
                <el-option
                v-for="item in getFparentno"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
              </el-select>
      </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
        <el-button
        type="primary"
          @click="handleFilter"
          >查找</el-button
        >
        <el-button
        type="success"
          @click="handleCreate"
          >添加</el-button
        >
      </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
       <!-- 查询结果 -->
    <JNPF-table
      v-loading="listLoading"
      :data="list"
    >
      <el-table-column align="center" :width="buttonColor.operationTwo" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button type="text" @click="handleUpdate(scope.row)">编辑</el-button>
         <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column
        :width="buttonColor.line"
        :index="indexMethod"
        align="center"
        label="序"
        type="index"
      />
      <el-table-column align="left" label="分类编号" prop="fclassstno" />
      <el-table-column align="left" label="分类名称" prop="fclassstname" />
      <el-table-column :formatter="formatFparentno" align="left" label="上级分类" prop="fparentno" />
    </JNPF-table>
    <paginations
    :total="total"
    :page.sync="listQuery.currentPage"
    :limit.sync="listQuery.pageSize"
    />
      </div>
    </div>
   <!-- 添加或修改对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      width="40%"
      :title="textMap[dialogStatus]"
    >
    <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form ref="dataForm" :rules="rules" :model="dataForm" @submit.native.prevent label-width="80px">
          <el-col :span="12">
            <el-form-item
                label="分类编号"
                prop="fclassstno"
              >
                <el-input :disabled="dialogStatus != 'create'" v-model="dataForm.fclassstno" />
              </el-form-item>
          </el-col>
          <el-col :span="12">
             <el-form-item label="分类名称" prop="fclassstname">
          <el-input v-model="dataForm.fclassstname" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上级分类" prop="fparentno">
           <el-select style="width:100%" clearable filterable v-model="dataForm.fparentno" class="filter-item" placeholder="请选择上级分类">
                <el-option
                v-for="item in getFparentno"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
              </el-select>
        </el-form-item>
          </el-col>
        </el-form>
    </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="info"  @click="dialogFormVisible = false">取消</el-button>
        <el-button
         v-if="dialogStatus == 'create'"
          @click="createData"
          type="primary"
          >确定</el-button
        >
        <el-button  v-else type="primary" @click="updateData">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listequipmentClass,
  createequipmentClass,
  updateequipmentClass,
  deleteequipmentClass,
} from "@/api/equipmentClass";
import { getListArea } from "@/api/area";
import Pagination from "@/components/Pagination";
import moment from "moment";
import { tableColor, titleColor, buttonColor } from "@/filters/index";
export default {
  name: "STsEquipmentClass",
  components: { Pagination },
  data() {
    return {
      buttonColor: buttonColor,
      getFparentno: null,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        fclassstname: undefined,
        fparentno: undefined,
        fclassstyle: undefined,
        flevel: undefined,
        fdiscmonth: undefined,
        fupddate: undefined,
        foperator: undefined,
        sort: "create_time",
        order: "desc",
      },
      dataForm: {
        fclassstno: undefined,
        fclassstname: undefined,
        fparentno: undefined,
        fclassstyle: undefined,
        flevel: undefined,
        fdiscmonth: undefined,
        fupddate: undefined,
        foperator: undefined,
        id: undefined,
      },
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "创建",
      },
      tableColor: tableColor,
      titleColor: titleColor,
      rules: {
        fclassstno: [{ required: true, message: "分类编号不能为空" }],
        fclassstname: [{ required: true, message: "分类名称不能为空" }],
        fparentno: [],
        fclassstyle: [
          {
            type: "number",
            message: "分类类别必须为数字值",
            transform: (value) => Number(value),
          },
        ],
        flevel: [
          {
            type: "number",
            message: "分类级别必须为数字值",
            transform: (value) => Number(value),
          },
        ],
        fdiscmonth: [
          {
            type: "number",
            message: "折扣年限必须为数字值",
            transform: (value) => Number(value),
          },
        ],
        fupddate: [],
        foperator: [],
      },
    };
  },
  created() {
    getListArea({ flag: 2 })
      .then((response) => {
        this.getFparentno = response.data.data.list;
      })
      .catch(() => {
        this.getFparentno = [];
      });
    this.getList();
  },
  methods: {
    formatFparentno: function (row, column) {
      if (this.getFparentno) {
        for (let i = 0; i < this.getFparentno.length; i++) {
          if (row.fparentno === this.getFparentno[i].value) {
            return this.getFparentno[i].label;
          }
        }
      } else {
        return "";
      }
    },
    datetimeFormat(row, column) {
      const date = row[column.property];
      if (date === undefined || date === null) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    getList() {
      this.listLoading = true;
      listequipmentClass(this.listQuery)
        .then((response) => {
          this.list = response.data.data.list;
          this.total = response.data.data.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.list = [];
          this.total = 0;
          this.listLoading = false;
        });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetForm() {
      this.dataForm = {
        fclassstno: undefined,
        fclassstname: undefined,
        fparentno: undefined,
        fclassstyle: undefined,
        flevel: undefined,
        fdiscmonth: undefined,
        fupddate: undefined,
        foperator: undefined,
        id: undefined,
      };
    },
    handleCreate() {
      this.resetForm();
      this.dialogStatus = "create";
      if (this.getFparentno.length > 0) {
        this.dataForm.fparentno = this.getFparentno[0].value;
      }
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id;
          this.dataForm.fupddate = new Date();
          createequipmentClass(this.dataForm)
            .then((response) => {
              this.list.unshift(response.data.data);
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "添加设备分类成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response,
              });
            });
        }
      });
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row);
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.dataForm.foperator = this.$store.state.user.id;
          this.dataForm.fupddate = new Date();
          updateequipmentClass(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.fclassstno === this.dataForm.fclassstno) {
                  const index = this.list.indexOf(v);
                  this.list.splice(index, 1, this.dataForm);
                  break;
                }
              }
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "更新设备分类成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response,
              });
            });
        }
      });
    },
    handleDelete(row) {
      this.$confirm("是否要删除设备分类", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteequipmentClass(row)
          .then((response) => {
            this.$notify.success({
              title: "成功",
              message: "删除设备分类成功",
            });
            const index = this.list.indexOf(row);
            this.list.splice(index, 1);
          })
          .catch((response) => {
            this.$notify.error({
              title: "失败",
              message: response,
            });
          });
      });
    },
    indexMethod(index) {
      index = index + 1;
      return index;
    },
  },
};
</script>